package com.hzcx.app.simplechat.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.publicui.WebViewActivity;
import com.hzcx.app.simplechat.ui.publicui.bean.ProtocolBean;
import com.hzcx.app.simplechat.ui.publicui.enums.SendCodeEnum;
import com.hzcx.app.simplechat.ui.setting.contract.UpdatePwdContract;
import com.hzcx.app.simplechat.ui.setting.presenter.UpdatePwdPresenter;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.view.MainBlueConfirmButton;
import com.hzcx.app.simplechat.view.MainPwdEditText;
import com.hzcx.app.simplechat.view.SendCodeTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改密码
 */
public class UpdatePwdActivity extends BaseActivity<UpdatePwdPresenter> implements TextWatcher, UpdatePwdContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_send_code)
    SendCodeTextView tvSendCode;
    @BindView(R.id.et_pwd)
    MainPwdEditText etPwd;
    @BindView(R.id.iv_pwd_state)
    ImageView ivPwdState;
    @BindView(R.id.btn_commit)
    MainBlueConfirmButton btnCommit;

    @Override
    public int getLayout() {
        return R.layout.activity_update_pwd;
    }

    @Override
    public void initPresenter() {
        mPresenter = new UpdatePwdPresenter();
    }

    @Override
    public void initView() {

        etPwd.setIvPwdState(ivPwdState);
        tvPhone.setText("验证码将发送至" + AppHelper.hidePhone(UserInfoUtil.getUserPhone()));

        etPwd.addTextChangedListener(this);
        etCode.addTextChangedListener(this);

    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.tv_send_code, R.id.btn_commit, R.id.tv_code_fail})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_send_code:
                mPresenter.sendCode(this, UserInfoUtil.getUserPhone(), SendCodeEnum.FORGET_PWD, UserInfoUtil.getUserPhoneCityCode());
                break;

            case R.id.btn_commit:
                mPresenter.updatePwd(this, etPwd.getText().toString(), etCode.getText().toString(), UserInfoUtil.getUserPhone());
                break;

            case R.id.tv_code_fail: // 收不到验证码
                mPresenter.getProtocol(this, "ver_code");
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    /**
     * 监听内容是否输入完成
     *
     * @param charSequence
     * @param i
     * @param i1
     * @param i2
     */
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (etCode.getText().toString().length() == 4 && etPwd.getText().toString().length() >= 6) {
            btnCommit.setBtnEnabled(true);
        } else {
            btnCommit.setBtnEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    /**
     * 验证码发送成功
     */
    @Override
    public void sendSuccess() {
        tvSendCode.start();
    }

    /**
     * 密码修改成功
     */
    @Override
    public void updateSuccess() {
        UserInfoBean userInfoBean = UserInfoUtil.getUserInfo();
        if (userInfoBean != null) {
            userInfoBean.setIs_password(1);
            UserInfoUtil.saveUser(userInfoBean);
        }
        showError("修改成功");
        finish();
    }

    /**
     * 收不到验证码地址获取成功
     *
     * @param data
     */
    @Override
    public void protocolResult(ProtocolBean data) {
        startActivity(new Intent(this, WebViewActivity.class)
                .putExtra(WebViewActivity.INTENT_TITLE, data.getName())
                .putExtra(WebViewActivity.INTENT_WEB_URL, data.getContenturl()));
    }
}
